<?php
/**
 * User: bim bip
 * Date: 7/27/11
 * Time: 2:10 PM
 */
class Comment extends CI_Controller{
    var $_message = '';
    var $_email = '';
    var $_name = '';
    function __construct(){
        parent::__construct();
        $this->load->model('movie/m_comment');
    }

    /**
     * add new comment
     * @return bool
     */
    function addNew($id,$type){
        //check post
        /*echo 'name' . $this->input->post('name');
        echo 'email' .$this->input->post('email');
        echo 'id' . $this->input->post('id');
        echo 'content' .$this->input->post('comment');
        echo 'submit' .$this->input->post('submit');*/
//        echo 'id=' . $id;

        if(!$this->input->post('submit') || !$this->input->post('name')
            || !$this->input->post('email')
            || !$this->input->post('comment')){
            echo 'ko hop le';
            return;
        }
        else{
            //add video comment
            if($type == 'video'){
                $content = htmlspecialchars($this->input->post('comment')); //convert special chars
                $email = htmlspecialchars($this->input->post('email'));
                $name = htmlspecialchars($this->input->post('name'));

                if(!$email || !$name){
                    $this->_email = $email;
                    $this->_name = $name;
                    return;
                }elseif(!$id){
                    return;
                }
                elseif(!$content){
                    $this->_email = $email;
                    $this->_name = $name;
                    return;
                }
                else{
                    $comment = new M_Comment();
                    $comment->_content = $content;
                    //replace <p> in content
                    $comment->_email = $this->input->post('email');
                    $comment->_movieID = $id;
                    $comment->_name = $this->input->post('name');
                    date_default_timezone_set('Asia/Ho_Chi_Minh'); //GMT + 7
                    $comment->_time = date('Y-m-d H:i:s');
                    //add new comment
                    $t = $comment->addNew();

                    if($t){ //neu add thanh cong
                        //parse smile tra ve
                        $this->load->helper('smile');
                         $this->load->model('site/smilemodel');

                        $searchArray = $this->smilemodel->getTextList();
                        $temp = $this->smilemodel->getPathList();
                        foreach($temp as &$item){
                            $item = "<img src='" . base_url() . $item . "' />";
                        }
                        $replaceArray = $temp;
                        echo parse_smile($searchArray, $replaceArray, $content);
                    }
                }

            }
        }
    }

    function video($movieID, $page){
        $offset = 0;
        $rowsPerPage = 5;
        if($page)
            $offset = ($page - 1) * 5;
        if($offset <   0) $offset = 0;
        //load model
        $this->load->model('movie/m_comment');

        $maxPageComment = 0;
        $content['comments'] = $this->m_comment->getComment($movieID, $offset, $rowsPerPage, $maxPageComment);
        $this->load->helper('smile'); //load smile helper
        //load search and replace smile
        $this->load->model('site/smilemodel');
        $content['smileList'] = $this->smilemodel->getSmileList();
        $content['searchArray'] = $this->smilemodel->getTextList();
        $temp = $this->smilemodel->getPathList();
        foreach($temp as &$item){
            $item = "<img src='" . base_url() . $item . "' />";
        }
        $content['replaceArray'] = $temp;
        $content['movie_id'] = $movieID;

        $this->load->helper('pagination');
        $content['endPage'] = $maxPageComment;
        if($page == 0) $page = 1;
        $content['pageArr'] = pageArray(2, $maxPageComment , $page);
        $content['currPage'] = $page;
        $this->load->view('movie/v_comment', $content);
    }

}
 
